9 if( !defined( 'MEDIAWIKI' ) )
12 require_once( 'Image.php' );
15 * Special handling for image description pages
18 class ImagePage
extends Article
{
20 /* private */ var $img; // Image object this page is shown for
24 $wgOut->setArticleBodyOnly(true);
25 $wgOut->addWikitext($this->getContent(true));
29 global $wgUseExternalEditor, $wgOut, $wgShowEXIF;
31 $this->img
= new Image( $this->mTitle
);
33 if( $this->mTitle
->getNamespace() == NS_IMAGE
) {
34 if ($wgShowEXIF && $this->img
->exists()) {
35 $exif = $this->img
->getExifData();
36 $showmeta = count($exif) ?
true : false;
42 if ($this->img
->exists())
43 $wgOut->addHTML($this->showTOC($showmeta));
45 $this->openShowImage();
47 $wgOut->addWikiText($this->makeMetadataTable($exif));
49 # No need to display noarticletext, we use our own message, output in openShowImage()
50 if( $this->getID() ) {
53 # Just need to set the right headers
54 $wgOut->setArticleFlag( true );
55 $wgOut->setRobotpolicy( 'index,follow' );
56 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
57 $wgOut->addMetaTags();
61 $this->closeShowImage();
62 $this->imageHistory();
74 * @param bool $metadata Whether or not to show the metadata link
77 function showTOC( $metadata ) {
79 $r = '<ul id="filetoc">
80 <li><a href="#file">' . $wgLang->getNsText( NS_IMAGE
) . '</a></li>' .
81 ($metadata ?
'<li><a href="#metadata">' . wfMsg( 'metadata' ) . '</a></li>' : '') . '
82 <li><a href="#filehistory">' . wfMsg( 'imghistory' ) . '</a></li>
83 <li><a href="#filelinks">' . wfMsg( 'imagelinks' ) . '</a></li>
89 * Make a table with metadata to be shown in the output page.
93 * @param array $exif The array containing the EXIF data
96 function makeMetadataTable( $exif ) {
97 $r = "{| class=metadata align=right width=250px\n";
98 $r .= '|+ id=metadata | '. htmlspecialchars( wfMsg( 'metadata' ) ) . "\n";
99 foreach( $exif as $k => $v ) {
100 $tag = strtolower( $k );
101 $r .= "! class=$tag |" . wfMsg( "exif-$tag" ) . "\n";
102 $r .= "| class=$tag |" . htmlspecialchars( $v ) . "\n";
105 return substr($r, 0, -3) . '|}';
109 * Overloading Article's getContent method.
110 * Omit noarticletext if sharedupload
112 * @param $noredir If true, do not follow redirects
114 function getContent( $noredir )
116 if ( $this->img
&& $this->img
->fromSharedDirectory
&& 0 == $this->getID() ) {
119 return Article
::getContent( $noredir );
122 function openShowImage()
124 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
125 $wgUseImageResize, $wgRepositoryBaseUrl,
126 $wgUseExternalEditor, $wgServer, $wgFetchCommonsDescriptions;
127 $full_url = $this->img
->getViewURL();
131 if( $wgUser->getOption( 'imagesize' ) == '' ) {
132 $sizeSel = User
::getDefaultOption( 'imagesize' );
134 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
136 if( !isset( $wgImageLimits[$sizeSel] ) ) {
137 $sizeSel = User
::getDefaultOption( 'imagesize' );
139 $max = $wgImageLimits[$sizeSel];
141 $maxHeight = $max[1];
142 $sk = $wgUser->getSkin();
144 if ( $this->img
->exists() ) {
146 $width = $this->img
->getWidth();
147 $height = $this->img
->getHeight();
150 if ( $this->img
->allowInlineDisplay() and $width and $height) {
153 # "Download high res version" link below the image
154 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img
->getSize()/1024 ) );
155 if ( $width > $maxWidth ) {
156 $height = floor( $height * $maxWidth / $width );
159 if ( $height > $maxHeight ) {
160 $width = floor( $width * $maxHeight / $height );
161 $height = $maxHeight;
163 if ( !$this->img
->mustRender()
164 && ( $width != $this->img
->getWidth() ||
$height != $this->img
->getHeight() ) ) {
165 if( $wgUseImageResize ) {
166 $thumbnail = $this->img
->getThumbnail( $width );
167 if ( $thumbnail == null ) {
170 $url = $thumbnail->getUrl();
173 # No resize ability? Show the full image, but scale
174 # it down in the browser so it fits on the page.
177 $anchoropen = "<a href=\"{$full_url}\">";
178 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
181 $showLink = $this->img
->mustRender();
183 $wgOut->addHTML( '<div class="fullImageLink" id="file">' . $anchoropen .
184 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
185 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>' );
187 #if direct link is allowed but it's not a renderable image, show an icon.
188 if ($this->img
->isSafeFile()) {
189 $icon= $this->img
->iconThumb();
191 $wgOut->addHTML( '<div class="fullImageLink" id="file"><a href="' . $full_url . '">' .
201 $s= $sk->makeMediaLink( $this->img
->getName(), '', '', true );
202 $info= wfMsg( 'fileinfo', ceil($this->img
->getSize()/1024.0), $this->img
->getMimeType() );
204 if (!$this->img
->isSafeFile()) {
205 $wgOut->addHTML("<div class=\"fullMedia\">");
206 $wgOut->addHTML("<span class=\"dangerousLink\">");
208 $wgOut->addHTML("</span>");
210 $wgOut->addHTML("<span class=\"fileInfo\"> (");
211 $wgOut->addWikiText( $info, false );
212 $wgOut->addHTML(")</span>");
213 $wgOut->addHTML("</div>");
215 #this should be formated a little nicer. Is CSS sufficient?
216 $wgOut->addHTML("<div class=\"mediaWarning\">");
217 $wgOut->addWikiText( wfMsg( 'mediawarning' ) );
218 $wgOut->addHTML('</div>');
221 $wgOut->addHTML("<div class=\"fullMedia\">");
224 $wgOut->addHTML("<span class=\"fileInfo\"> (");
225 $wgOut->addWikiText( $info, false );
226 $wgOut->addHTML(")</span>");
228 $wgOut->addHTML("</div>");
232 if($this->img
->fromSharedDirectory
) {
233 $this->printSharedImageText();
236 # Image does not exist
237 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
241 function printSharedImageText() {
242 global $wgRepositoryBaseUrl, $wgFetchCommonsDescriptions, $wgOut;
244 $url = $wgRepositoryBaseUrl . urlencode($this->mTitle
->getDBkey());
245 $sharedtext = "<div class='sharedUploadNotice'>" . wfMsg("sharedupload");
246 if ($wgRepositoryBaseUrl && !$wgFetchCommonsDescriptions) {
247 $sharedtext .= " " . wfMsg("shareduploadwiki", $url);
249 $sharedtext .= "</div>";
250 $wgOut->addWikiText($sharedtext);
252 if ($wgRepositoryBaseUrl && $wgFetchCommonsDescriptions) {
253 $ur = ini_set('allow_url_fopen', true);
254 $text = @file_get_contents
($url . '?action=render');
255 ini_set('allow_url_fopen', $ur);
257 $wgOut->addHTML($text);
261 function getUploadUrl() {
263 $uploadTitle = Title
::makeTitle( NS_SPECIAL
, 'Upload' );
264 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img
->getName() ) );
268 function uploadLinksBox()
270 global $wgUser, $wgOut;
272 if ($this->img
->fromSharedDirectory
)
275 $sk = $wgUser->getSkin();
276 $wgOut->addHTML( '<br /><ul><li>' );
277 $wgOut->addWikiText( '<div>'. wfMsg( 'uploadnewversion', $this->getUploadUrl() ) .'</div>' );
278 $wgOut->addHTML( '</li><li>' );
279 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle
,
280 wfMsg( 'edit-externally' ), "action=edit&externaledit=true&mode=file" ) );
281 $wgOut->addWikiText( '<div>' . wfMsg('edit-externally-help') . '</div>' );
282 $wgOut->addHTML( '</li></ul>' );
285 function closeShowImage()
292 * If the page we've just displayed is in the "Image" namespace,
293 * we follow it with an upload history of the image and its usage.
295 function imageHistory()
297 global $wgUser, $wgOut, $wgUseExternalEditor;
299 $sk = $wgUser->getSkin();
301 $line = $this->img
->nextHistoryLine();
304 $list =& new ImageHistoryList( $sk );
305 $s = $list->beginImageHistoryList() .
306 $list->imageHistoryLine( true, $line->img_timestamp
,
307 $this->mTitle
->getDBkey(), $line->img_user
,
308 $line->img_user_text
, $line->img_size
, $line->img_description
);
310 while ( $line = $this->img
->nextHistoryLine() ) {
311 $s .= $list->imageHistoryLine( false, $line->img_timestamp
,
312 $line->oi_archive_name
, $line->img_user
,
313 $line->img_user_text
, $line->img_size
, $line->img_description
);
315 $s .= $list->endImageHistoryList();
317 $wgOut->addHTML( $s );
319 # Exist check because we don't want to show this on pages where an image
320 # doesn't exist along with the noimage message, that would suck. -ævar
321 if( $wgUseExternalEditor && $this->img
->exists() ) {
322 $this->uploadLinksBox();
327 function imageLinks()
329 global $wgUser, $wgOut;
331 $wgOut->addHTML( '<h2 id="filelinks">' . wfMsg( 'imagelinks' ) . "</h2>\n" );
333 $dbr =& wfGetDB( DB_SLAVE
);
334 $page = $dbr->tableName( 'page' );
335 $imagelinks = $dbr->tableName( 'imagelinks' );
337 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
338 $dbr->addQuotes( $this->mTitle
->getDBkey() ) . " AND il_from=page_id"
339 . " LIMIT 500"; # quickie emergency brake
340 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
342 if ( 0 == $dbr->numRows( $res ) ) {
343 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
346 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
348 $sk = $wgUser->getSkin();
349 while ( $s = $dbr->fetchObject( $res ) ) {
350 $name = Title
::MakeTitle( $s->page_namespace
, $s->page_title
);
351 $link = $sk->makeKnownLinkObj( $name, "" );
352 $wgOut->addHTML( "<li>{$link}</li>\n" );
354 $wgOut->addHTML( "</ul>\n" );
359 global $wgUser, $wgOut, $wgRequest;
361 $confirm = $wgRequest->getBool( 'wpConfirmB' );
362 $image = $wgRequest->getVal( 'image' );
363 $oldimage = $wgRequest->getVal( 'oldimage' );
365 # Only sysops can delete images. Previously ordinary users could delete
366 # old revisions, but this is no longer the case.
367 if ( !$wgUser->isAllowed('delete') ) {
368 $wgOut->sysopRequired();
371 if ( $wgUser->isBlocked() ) {
372 return $this->blockedIPpage();
374 if ( wfReadOnly() ) {
375 $wgOut->readOnlyPage();
379 # Better double-check that it hasn't been deleted yet!
380 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
381 if ( ( !is_null( $image ) )
382 && ( '' == trim( $image ) ) ) {
383 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
387 $this->img
= new Image( $this->mTitle
);
389 # Deleting old images doesn't require confirmation
390 if ( !is_null( $oldimage ) ||
$confirm ) {
391 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
394 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
399 if ( !is_null( $image ) ) {
400 $q = '&image=' . urlencode( $image );
401 } else if ( !is_null( $oldimage ) ) {
402 $q = '&oldimage=' . urlencode( $oldimage );
406 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
411 global $wgOut, $wgUser, $wgContLang, $wgRequest;
412 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
413 $fname = 'ImagePage::doDelete';
415 $reason = $wgRequest->getVal( 'wpReason' );
416 $oldimage = $wgRequest->getVal( 'oldimage' );
418 $dbw =& wfGetDB( DB_MASTER
);
420 if ( !is_null( $oldimage ) ) {
421 if ( strlen( $oldimage ) < 16 ) {
422 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
425 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
426 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
430 # Invalidate description page cache
431 $this->mTitle
->invalidateCache();
436 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
437 $wgInternalServer.$this->mTitle
->getFullURL()
439 wfPurgeSquidServers($urlArr);
441 $this->doDeleteOldImage( $oldimage );
442 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
443 $deleted = $oldimage;
445 $image = $this->mTitle
->getDBkey();
446 $dest = wfImageDir( $image );
447 $archive = wfImageDir( $image );
449 # Delete the image file if it exists; due to sync problems
450 # or manual trimming sometimes the file will be missing.
451 $targetFile = "{$dest}/{$image}";
452 if( file_exists( $targetFile ) && ! @unlink
( $targetFile ) ) {
453 # If the deletion operation actually failed, bug out:
454 $wgOut->fileDeleteError( $targetFile );
457 $dbw->delete( 'image', array( 'img_name' => $image ) );
458 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
460 # Purge archive URLs from the squid
462 while ( $s = $dbw->fetchObject( $res ) ) {
463 $this->doDeleteOldImage( $s->oi_archive_name
);
464 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name
);
467 # And also the HTML of all pages using this image
468 $linksTo = $this->img
->getLinksTo();
470 $u = SquidUpdate
::newFromTitles( $linksTo, $urlArr );
471 array_push( $wgPostCommitUpdateList, $u );
474 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
476 # Image itself is now gone, and database is cleaned.
477 # Now we remove the image description page.
479 $article = new Article( $this->mTitle
);
480 $article->doDeleteArticle( $reason ); # ignore errors
482 # Invalidate parser cache and client cache for pages using this image
483 # This is left until relatively late to reduce lock time
484 Title
::touchArray( $linksTo );
486 /* Delete thumbnails and refresh image metadata cache */
487 $this->img
->purgeCache();
493 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
494 $wgOut->setRobotpolicy( 'noindex,nofollow' );
496 $loglink = '[[Special:Log/delete|' . wfMsg( 'deletionlog' ) . ']]';
497 $text = wfMsg( 'deletedtext', $deleted, $loglink );
499 $wgOut->addWikiText( $text );
501 $wgOut->returnToMain( false, $this->mTitle
->getPrefixedText() );
504 function doDeleteOldImage( $oldimage )
508 $name = substr( $oldimage, 15 );
509 $archive = wfImageArchiveDir( $name );
511 # Delete the image if it exists. Sometimes the file will be missing
512 # due to manual intervention or weird sync problems; treat that
513 # condition gracefully and continue to delete the database entry.
514 # Also some records may end up with an empty oi_archive_name field
515 # if the original file was missing when a new upload was made;
516 # don't try to delete the directory then!
518 $targetFile = "{$archive}/{$oldimage}";
519 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink
( $targetFile ) ) {
520 # If we actually have a file and can't delete it, throw an error.
521 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
524 $log = new LogPage( 'delete' );
525 $log->addEntry( 'delete', $this->mTitle
, wfMsg('deletedrevision',$oldimage) );
531 global $wgOut, $wgRequest, $wgUser;
532 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
534 $oldimage = $wgRequest->getText( 'oldimage' );
535 if ( strlen( $oldimage ) < 16 ) {
536 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
539 if ( strstr( $oldimage, "/" ) ||
strstr( $oldimage, "\\" ) ) {
540 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
544 if ( wfReadOnly() ) {
545 $wgOut->readOnlyPage();
548 if( $wgUser->isAnon() ) {
549 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
552 if ( ! $this->mTitle
->userCanEdit() ) {
553 $wgOut->sysopRequired();
556 if ( $wgUser->isBlocked() ) {
557 return $this->blockedIPpage();
559 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
560 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
563 $name = substr( $oldimage, 15 );
565 $dest = wfImageDir( $name );
566 $archive = wfImageArchiveDir( $name );
567 $curfile = "{$dest}/{$name}";
569 if ( ! is_file( $curfile ) ) {
570 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
573 $oldver = wfTimestampNow() . "!{$name}";
575 $dbr =& wfGetDB( DB_SLAVE
);
576 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
578 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
579 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
582 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
583 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
586 # Record upload and update metadata cache
587 $img = Image
::newFromName( $name );
588 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
590 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
591 $wgOut->setRobotpolicy( 'noindex,nofollow' );
592 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
594 $descTitle = $img->getTitle();
595 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
598 function blockedIPpage() {
599 require_once( 'EditPage.php' );
600 $edit = new EditPage( $this );
601 return $edit->blockedIPpage();
610 class ImageHistoryList
{
611 function ImageHistoryList( &$skin ) {
612 $this->skin
=& $skin;
615 function beginImageHistoryList() {
616 $s = "\n<h2 id=\"filehistory\">" . wfMsg( 'imghistory' ) . "</h2>\n" .
617 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
621 function endImageHistoryList() {
626 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
627 global $wgUser, $wgLang, $wgContLang, $wgTitle;
629 $datetime = $wgLang->timeanddate( $timestamp, true );
630 $del = wfMsg( 'deleteimg' );
631 $delall = wfMsg( 'deleteimgcompletely' );
632 $cur = wfMsg( 'cur' );
635 $url = Image
::imageUrl( $img );
637 if ( $wgUser->isAllowed('delete') ) {
638 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
640 $style = $this->skin
->getInternalLinkAttributes( $link, $delall );
642 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
647 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
648 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
649 $token = urlencode( $wgUser->editToken( $img ) );
650 $rlink = $this->skin
->makeKnownLinkObj( $wgTitle,
651 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
652 urlencode( $img ) . "&wpEditToken=$token" );
653 $dlink = $this->skin
->makeKnownLinkObj( $wgTitle,
654 $del, 'action=delete&oldimage=' . urlencode( $img ) .
655 "&wpEditToken=$token" );
657 # Having live active links for non-logged in users
658 # means that bots and spiders crawling our site can
659 # inadvertently change content. Baaaad idea.
660 $rlink = wfMsg( 'revertimg' );
665 $userlink = $usertext;
667 $userlink = $this->skin
->makeLinkObj(
668 Title
::makeTitle( NS_USER
, $usertext ),
671 $nbytes = wfMsg( 'nbytes', $size );
672 $style = $this->skin
->getInternalLinkAttributes( $url, $datetime );
674 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
675 . " . . {$userlink} ({$nbytes})";
677 $s .= $this->skin
->commentBlock( $description, $wgTitle );